/*
* Option Edit JS
* This creates the admin GUI Option Unit Type Plugin
*
* JSLint.com Check: 18/03/2011
*/
RM.Pages.Option_Edit_Grid_Create_Row = Ext.data.Record.create([
    {name: 'id', type: 'string'}
]);

RM.Pages.Functions.option_edit_save = function(){
    var records = RM.Pages.Option_Edit_Json_Store.getModifiedRecords();
    if(!records.length) {
        return;
    }

    var myMask = new Ext.LoadMask('content-panel', {msg:RM.Translate.Common.PleaseWait});
    myMask.show();

    var data = [];
    Ext.each(records, function(r, i) {
        var o = r.getChanges();
        if(r.data.newRecord) {
            o.newRecord = true;
        }
        o.id = r.get('id');
        data.push(o);
    }, this);

    var o = {
         url:RM.Common.AssembleURL({
            controller : 'Option',
            action: 'updateJson'
         })
        ,method:'post'
        ,scope:this
        ,params:{
            data: Ext.encode(data)
        }
        ,success: function(responseObject) {
            myMask.hide();
            RM.Pages.Option_Edit_Json_Store.load();
        },
        failure: function() {
            myMask.hide();
        }
    };
    Ext.Ajax.request(o);
};

RM.Pages.Option_Edit_Toolbar = {
    xtype : "panel",
    id : "rm_pages_option_edit_toolbar",
    bodyBorder : false,
    html : RM.Common.getToolbar([
        {image: RM.BaseLargeImageURL+"save.gif", label: RM.Translate.Common.Save, link: "RM.Pages.Functions.option_edit_save()"},
        {image: RM.BaseLargeImageURL+"delete.gif", label: RM.Translate.Common.Delete, link: "RM.Pages.Functions.option_edit_config()"}
    ])
};
RM.Toolbars.push(RM.Pages.Option_Edit_Toolbar);

RM.Pages.Option_Edit_Json_Store = new Ext.data.JsonStore({
    url: RM.Common.AssembleURL({
        controller: 'Option',
        action: 'listJson'
    }),
    id: 'rm_option_edit_grid_json_store',
    totalProperty: 'total',
    root: 'data',
    pruneModifiedRecords: true,
    fields: [
        {name: "id"},
        {name: "name"},
        {name: "units" }
    ],
    sortInfo: {field: 'id', direction: 'ASC'},
    remoteSort: true
});



RM.Pages.Option_Edit_Columns_SM = new Ext.grid.CheckboxSelectionModel();
RM.Pages.Option_Edit_Columns_Rows = [
    RM.Pages.Option_Edit_Columns_SM,
    {dataIndex: 'id', header: RM.Translate.Common.Id},
    {
        id: 'name',
        dataIndex: 'name',
        header: RM.Translate.Admin.Option.Edit.OptionName,
        sortable: false,
        editor: new Ext.form.TextField({
            allowBlank: false
        })
    },
    {
        dataIndex: "units",
        header: RM.Translate.Admin.Option.Edit.SelectedUnits,
        editor: new Ext.ux.form.CheckboxCombo({
            width: 250,
            mode: 'local',
            store: new Ext.data.JsonStore({
                url: RM.Common.AssembleURL({
                    controller: 'Option',
                    action: 'unitlistJson'
                }),
                autoLoad: true,
                sortInfo: { field: 'name', direction: "ASC"},
                idProperty : 'id',
                fields : [
                    {name:'id', type:'string'},
                    {name:'name', type:'string'}
                ]
            }),
            valueField: 'id',
            displayField: 'name',
            allowBlank: false
        }),
        renderer: function(value, metaData, record, rowIndex, colIndex, store){
            if (value=="0"){
                return RM.Translate.Common.AllUnits;
            } else {
                return value;
            }
        }
    }
];
RM.Pages.Option_Edit_Columns = new Ext.grid.ColumnModel({
    columns: RM.Pages.Option_Edit_Columns_Rows,
    defaults: {
        sortable: true
    }
});

RM.Pages.Option_Edit_Grid = new Ext.grid.EditorGridPanel({
    id : 'rm_option_edit_grid',
    clicksToEdit: 1,
    stripeRows: true,
    height: RM.Common.GetPanelHeight(136),
    bbar : new Ext.PagingToolbar({
        store: RM.Pages.Option_Edit_Json_Store,
        pageSize: 15
    }),
    remoteSort: true,
    enableColLock : false,
    loadMask : {
        msg: RM.Translate.Common.PleaseWait
    },
    cm : RM.Pages.Option_Edit_Columns,
    store : RM.Pages.Option_Edit_Json_Store,
    sm : RM.Pages.Option_Edit_Columns_SM,
    viewConfig: {
        forceFit: true
    },
    autoExpandColumn: "name"
});

RM.Pages.Option_Edit = new Ext.Panel({
    id : 'rm_pages_option_edit',
    layout : 'form',
    iconCls: "RM_config_option_icon",
    title: RM.Translate.Admin.Option.Main.Title,
    items : [
        RM.Pages.Option_Edit_Grid
    ]
});

RM.Pages.Functions.Option_ListJson = function () {
    Ext.getCmp('toolbar_panel').layout.setActiveItem('rm_pages_option_edit_toolbar');
    Ext.getCmp('content-panel').layout.setActiveItem('rm_pages_option_edit');
    RM.Pages.Option_Edit_Json_Store.load();
};

RM.Main.Pages.push(RM.Pages.Option_Edit);